/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uloha3;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import uloha1.Riesenie;

public class DataParser {

    private String nazovVstupu;

    DataParser(String vstupnySubor) {
        nazovVstupu = vstupnySubor;
    }

/******************************************************************************/
    
    List<Batoh> nacitajVstupnySuborSInstanciou() {
        List<Batoh> zoznamInstancii = new ArrayList<Batoh>();
        File nacitavanySubor;
        FileInputStream fstream = null;
        DataInputStream in = null;
        BufferedReader br = null;

        String nacitaneData;
        String kopiaDat;

        double cenaVeci = 0.0;
        double vahaVeci = 0.0;
        int cisloInstance = 0;
        int pocetVeciVBatohu = 0;
        int maxNosnostBatohu = 0;
        try {
        	System.out.println("NAZOV VSTUPU je" + nazovVstupu);
            fstream = new FileInputStream(new File(nazovVstupu));
            in = new DataInputStream(fstream);
            br = new BufferedReader(new InputStreamReader(in));
            while ((nacitaneData = br.readLine()) != (null)) {
                Batoh aktualnaInstancia = null;
                Scanner sc = new Scanner(nacitaneData).useDelimiter(" ");
                cisloInstance = sc.nextInt();
                pocetVeciVBatohu = sc.nextInt();
                //System.out.println("Veci je " +pocetVeciVBatohu);
                maxNosnostBatohu = sc.nextInt();
                aktualnaInstancia = new Batoh(maxNosnostBatohu, pocetVeciVBatohu, cisloInstance);
                for (int i = 0; i < pocetVeciVBatohu; i++) {
                    vahaVeci = sc.nextDouble();
                    cenaVeci = sc.nextDouble();
                    VecDoBatohu vkladanaVec = new VecDoBatohu(cenaVeci, vahaVeci, ((double) cenaVeci) / ((double) vahaVeci), i);
                    aktualnaInstancia.pridajVecDoBatohu(vkladanaVec);
                } //vsetky veci su uz priradene do batohu, instancia je nacitana
                zoznamInstancii.add(aktualnaInstancia);
            //System.out.println("Batoh " + aktualnaInstancia.maxHmotnost + " " + aktualnaInstancia.pocetVeciVbatohu + "  bol prave vytvoreny");
            }
            return zoznamInstancii;
        } catch (FileNotFoundException ex) {
            System.out.println("Subor zadany ako argument nebol najdeny");
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (Exception ex) {
            System.out.println("Some exception");
            return null;
        } finally {
            try {
                fstream.close();
                in.close();
                br.close();

            } catch (IOException ex) {
                Logger.getLogger(Riesenie.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }

    }
    
/******************************************************************************/
    
    public String getNazovVstupu() {
        return nazovVstupu;
    }

    public void setNazovVstupu(String nazovVstupu) {
        this.nazovVstupu = nazovVstupu;
    }
    
/******************************************************************************/    
    

}
